﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.Model;

namespace FantastPlatform.Community.UI
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string redirectUrl = "";
            try
            {
                int totalcout = 0;
                if (!string.IsNullOrEmpty(Request["idCard"]))
                {
                    string idCard = Request["idCard"];
                    IEmployee employee = new Employee();
                    IEmployeeService employeeService = ServiceFactory.CreateService<IEmployeeService>();
                    ConditionGroup group = new ConditionGroup();
                    Condition enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    group.Conditions.Add(enableCondition);
                    Condition idcardCondition = new Condition() { Type = typeof(string).FullName, Field = "IdCard", Value1 = idCard, Comparison = Comparisons.Eq };
                    group.Conditions.Add(idcardCondition);
                    IList<Sort> sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    IList<IEmployee> employeelist = employeeService.Search(group, sorts, 1, 1, out totalcout);
                    if (employeelist.Count > 0)
                    {
                        employee = employeelist[0];
                        IList<IEmployeePost> employeePosts = employeelist[0].EmployeePosts;
                        employee.EmployeePosts = new List<IEmployeePost>();
                        IEmployeePost employeepost = (from p in employeePosts
                                                      where p.Enable == true
                                                      select p).SingleOrDefault();
                        employee.EmployeePosts.Add(employeepost);
                        Session["User"] = employee;
                        Session["PolicemanName"] = employee.Name;
                        redirectUrl = "Default.aspx";
                    }
                    else
                    {
                        redirectUrl = "SSLLogin.aspx";
                    }
                }
                else
                {
                    redirectUrl = "Error.aspx";
                }
            }
            catch (Exception ex)
            {
                redirectUrl = "Error.aspx";
            }
            if (!string.IsNullOrEmpty(redirectUrl))
            {
                Response.Redirect(redirectUrl);
            }
            else
            {
                Response.Redirect(AppUrl + "/Error.aspx");
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            
        }
    }
}
